/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import stendcontrol.CoefConfigurator;
import stendcontrol.Configurator;
import stendcontrol.DBServer;
import stendcontrol.DataProcessor;
import stendcontrol.Experiment;
import stendcontrol.MessageBuilder;
import stendcontrol.SensorInformer;
import stendcontrol.UdpServer;

/**
 *
 * @author RATSUF
 */
public class ExperimentOutputStream_Test {

    public static void main(String[] args) throws SocketException,
            UnknownHostException, InterruptedException, FileNotFoundException,
            IOException {

        Configurator cnf = Configurator.getConf();
        CoefConfigurator coef_cnf = CoefConfigurator.getConf();
        UDP_Fluder udp_flud = new UDP_Fluder("127.0.0.255", 1);
        UdpServer udp_serv = new UdpServer("127.0.0.255",
                (Integer) cnf.getProperty(Configurator.UDP_IN_PORT),
                (Integer) cnf.getProperty(Configurator.UDP_OUT_PORT));
        MessageBuilder mb = new MessageBuilder();
        DataProcessor dp = new DataProcessor();
        Experiment exp = new Experiment();
        final SensorInformer si = new SensorInformer(coef_cnf.getSensorCoef0(),
                coef_cnf.getSensorCoefProp());

        Thread t_udp_flud = new Thread(udp_flud);
        Thread t_udp_serv = new Thread(udp_serv);
        Thread t_mb = new Thread(mb);




        udp_serv.addTransmitListener(mb);
        udp_serv.addTransmitListener(si);
        mb.addMessageListener(exp);

        t_mb.start();


        udp_serv.startTransmitToExp();
        t_udp_serv.start();

        t_udp_flud.start();

        Thread.sleep(1000);


        udp_flud.shutdown();
        udp_serv.stopUDPThread();
        mb.stopBuilding();

        System.out.println("flud wait...");
        t_udp_flud.join();
        System.out.println("serv wait...");
        t_udp_serv.join();
        System.out.println("mb wait...");
        t_mb.join();

        exp.close();
        exp.getPositions();
        for (int i = 0; i < 3; i++) {
            exp.getProcess(i);

        }


        exp.setExpName("bieber_terue");
        
       Experiment objFromDatabase = null ;
        Experiment my_exp = exp;

        DBServer db =null;
        try {
            db = new DBServer();
        } catch (SQLException ex) {
            Logger.getLogger(ExperimentOutputStream_Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ExperimentOutputStream_Test.class.getName()).log(Level.SEVERE, null, ex);
        }
      //  db.createConnection();
        Connection con = db.getCon();
      
        System.out.println("Before serialize exp name : " + exp.getExpName());
        System.out.println("After serialize information : " + exp.getCountMeasures());  
        
        
        long serialized_id;
        try {
            serialized_id = SerializeToBase
                    .serializeJavaObjectToDB(con, exp);
            System.out.println("Serialized id ->> " + serialized_id);   
            
            objFromDatabase = (Experiment) SerializeToBase.
                    deSerializeJavaObjectFromDB(
                    con, serialized_id);
      
        } catch (SQLException ex) {
            Logger.getLogger(ExperimentOutputStream_Test.
                    class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ExperimentOutputStream_Test.
                    class.getName()).log(Level.SEVERE, null, ex);
        }

        // de-serializing java object from mysql database





     

        System.out.println("Exp name : " + objFromDatabase .getExpName());
        System.out.println("Other information : " + objFromDatabase.getCountMeasures());
        db.closeConnection();



        /*   FileServer fs = new FileServer("E:\\Test");
            
         Experiment ex = fs.loadExp(0);
         System.out.println("Exp name: " + ex.getExpName())*/





    }
}
